#!/usr/bin/perl

use strict;

package main;

use Getopt::Long;
use List::Util qw(first);
use Lunmgr ':all';

my %_cmds = (
    mount => '/bin/mount',
    umount => '/bin/umount',
);

my %_opts;

GetOptions(\%_opts,
          "-umount",
          "-all",
          "-help|h");

my @pathes = $_opts{all} ? () : split /,/, join(',', @ARGV);

my @devices = ReadTable("/home/lunmgr/lunmgr.devtable", @pathes);

foreach my $device (@devices) {
    next if ($device->mount_pt eq "NONE");

    if ($device->mpath) {
        my $cmd = $_opts{umount} 
                  ? $_cmds{umount} . " " . $device->path . "(/dev/mapper/" . $device->mpath . "p1)"
                  : $_cmds{mount} . " /dev/mapper/" . $device->mpath . "p1 " 
                    . $device->path; 
        print "$cmd\n";
    }
    elsif ($device->dev) {
        my $cmd = $_opts{umount} 
                  ? $_cmds{umount} . $device->path . "(" . $device->dev . "p1)"
                  : join(" ", ($_cmds{mount}, $device->dev, $device->path)); 
        print "$cmd\n";
    }
    else {
        print STDERR "Path " . $device->path . " is not visible for this cabinet\n";
    }
}

exit(0);
